//
//  CertificateMailModel.m
//  VideoExam
//
//  Created by apple on 2020/4/17.
//

#import "CertificateMailModel.h"

@implementation CertificateMailModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        _cellHeight=50;
    }
    return self;
}
/// 获取配置好的model 数据
/// @param orderId orderId
/// @param successBlock 配置好的model 数据
//+ (void)get_configuration_model_orderId:(NSString *)orderId SuccessBlock:(void(^)(CertificateMailModel * nmodel, BOOL editor))successBlock{
//    /*
//     逻辑说明：
//     先获取当前订单里的地址信息，看看有没有； 没有就在请求上次订单的地址信息
//     */
//    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
//    dispatch_semaphore_t  semaphore = dispatch_semaphore_create(0);
//
//   __block BOOL iseditor = YES; //界面是否编辑
//   __block CertificateMailModel *model =nil;
//    dispatch_async(queue, ^{
//        //获取当前订单 里的地址
//        [CertificateMailModel get_address_orderId:orderId Success:^(id obj) {
//            if (obj) {
//                model = (CertificateMailModel *)obj;
//                iseditor = NO;//有数据代表填写过这个订单的地址 返回NO 不可编辑
//            }
//            dispatch_semaphore_signal(semaphore);
//        } error:^{
//            dispatch_semaphore_signal(semaphore);
//        }];
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//
//        if (model==nil) {
//            //获取邮寄地址信息
//            [CertificateMailModel get_last_orderId_address_info_Success:^(id obj) {
//                 model = [CertificateMailModel yy_modelWithJSON:obj];
//                 dispatch_semaphore_signal(semaphore);
//            } error:^{
//                 dispatch_semaphore_signal(semaphore);
//            }];
//            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//         }
//
//         dispatch_async(dispatch_get_main_queue(), ^{
//             if (model==nil) {
//                 model = [[CertificateMailModel alloc] init];
//             }
//            if (successBlock) {
//                successBlock(model,iseditor);
//            }
//        });
//    });
//}



#pragma mark 获取邮寄地址信息
//获取邮寄地址信息
+ (void) get_order_address_info_orderId:(NSString *)orderId Success:(ObjSuccess)success error:(ErrorBlock)errorBlock {
    NSString * url = [NSString stringWithFormat:@"%@%@",get_order_address_url,orderId];
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
        NSLog(@"获取当前用户邮寄地址信息 %@",responseObject);
        if ([AppTools isStateSuccess:responseObject]) {
            CertificateMailModel * model = nil;
            if ([AppTools is_parsing_security_responseObject:responseObject]) {
                model = [CertificateMailModel yy_modelWithJSON:responseObject[@"data"]];
            }
          if (success) {
              success(model);
           }
        }
    } failure:^(NSError *error) {
        NSLog(@"获取当前用户邮寄地址信息error %@",error);
        NSString *errorStr = [self errorDescribe:error];
        [QMUITips showError:errorStr];
    }];
}

#pragma mark 添加邮寄地址
/// 添加邮寄地址
/// @param success success
/// @param errorBlock errorBlock
-(void)add_addressSuccess:(ObjSuccess)success error:(ErrorBlock)errorBlock {
    NSDictionary *dic=[self yy_modelToJSONObject];
    NSLog(@"提交订单号 %@",dic[@"orderId"]);
    [NetWorkTools jsonFormat_requestURLStr:add_address_url params:dic success:^(id responseObject) {
        NSLog(@"添加邮寄地址 %@",responseObject);
        if ([AppTools isStateSuccess:responseObject]) {
            if (success) {
                success(nil);
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"添加邮寄地址 error%@",error);
        if (errorBlock) {
            errorBlock();
        }
        NSString *errorStr = [self errorDescribe:error];
        [QMUITips showError:errorStr];
    }];
}

//#pragma mark 根据订单号获取订单信息
///// 根据订单号获取订单信息
///// @param orderId 订单id
///// @param success CertificateMailModel
///// @param errorBlock errorBlock
//+ (void)get_address_orderId:(NSString *)orderId Success:(ObjSuccess)success error:(ErrorBlock)errorBlock {
//    NSString *url = [NSString stringWithFormat:@"%@%@",get_address_info_url,orderId];
//    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
//        NSLog(@"根据订单获取邮寄地址 %@",responseObject);
//        if ([AppTools isStateSuccess:responseObject]) {
//            CertificateMailModel *model = [CertificateMailModel yy_modelWithJSON:responseObject[@"data"]];
//            if (success) {
//                if ([model is_available]) {
//                    success(model);
//                } else {
//                    success(nil);
//                }
//            }
//        } else {
//            if (errorBlock) {
//                errorBlock();
//            }
//        }
//    } failure:^(NSError *error) {
//        NSLog(@"根据订单获取邮寄地址error %@",error);
//
//        if (errorBlock) {
//            errorBlock();
//        }
//    }];
//}




-(BOOL)data_validation {
    if ([self.name isHelpful]==NO) {
        return NO;
    }
    if (self.name.length<=1 || self.name.length>10) {
         return NO;
    }
    if ([self.phone isHelpful]==NO) {
       return NO;
    }
    if ([NSString verifyPhoneNumber:self.phone]==NO) {
           return NO;
       }
    if ([self.city isHelpful]==NO) {
        return NO;
    }
    if ([self.address isHelpful]==NO) {
        return NO;
    }
    return YES;
}

//根据数据判断是否可编辑 有数据就不可编辑
- (BOOL)is_editor{
    if ([self.name isHelpful]==NO || [self.phone isHelpful]==NO || [self.address isHelpful]==NO  || [self.city isHelpful]==NO || [self.address isHelpful]==NO) {
        return YES;
    }
    return NO;
}
//判断数据是否可用
- (BOOL)is_available {
    if ([self.name isHelpful]==NO || [self.phone isHelpful]==NO || [self.address isHelpful]==NO  || [self.city isHelpful]==NO || [self.address isHelpful]==NO) {
        return NO;
    }
    return YES;
}
@end
